import dayjs from "dayjs";
// 补0
export const prefixInteger = (num = 0, n = 4) => {
  if (!num) return "-";
  return (Array(n).join(0) + num).slice(-n);
};

// 空值默认
export const isEmpty = val => {
  if (val === 0) return val;
  if (!val) return "-";
  return val;
};

// 字符串超长省略
export const beyondReplace = (val, n = 6) => {
  if (!val) return "-";
  return val.length > n ? val.slice(0, n) + "..." : val;
};

// 保留小数
export const toFixed = (val, num = 2) => {
  if (!val) return 0;
  return Number(val).toFixed(num);
};

/**
 * 日期格式化
 * @param {String,Date} date
 * @param {String} format {YYYY-MM-DD}
 */
export const dateFormat = (date, format = "YYYY-MM-DD") => {
  if (!date) return "-";
  return dayjs(date).format(format);
};
